package offer.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author DengYuan2
 * @create 2021-02-26 20:21
 */
public class n_27 {
    public static void main(String[] args) {
        n_27 n = new n_27();
        TreeNode t1 = new TreeNode(8);
        TreeNode t2 = new TreeNode(6);
        TreeNode t3 = new TreeNode(10);
        TreeNode t4 = new TreeNode(5);
        TreeNode t5 = new TreeNode(7);
        TreeNode t6 = new TreeNode(9);
        TreeNode t7 = new TreeNode(11);
        t1.left=t2;
        t1.right=t3;
        t2.left=t4;
        t2.right=t5;
        t3.left=t6;
        t3.right=t7;
        n.Mirror(t1);
        n.layerOrder(t1);
    }

    public void Mirror(TreeNode root) {
        if (root==null){
            return;
        }
        TreeNode tmp = root.left;
        root.left=root.right;
        root.right=tmp;
        Mirror(root.left);
        Mirror(root.right);
    }

    /**
     * 层次遍历
     *
     * @param node
     */
    public void layerOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        int count = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(node);
        TreeNode tmp = null;
        while (!queue.isEmpty()) {
            count = queue.size();
            for (int i = 0; i < count; i++) {
                tmp = queue.poll();
                System.out.print(tmp.val + " ");
                if (tmp.left != null) {
                    queue.offer(tmp.left);
                    count++;
                }
                if (tmp.right != null) {
                    queue.offer(tmp.right);
                    count++;
                }
            }
        }
    }
}
